/*
 * @lc app=leetcode.cn id=4 lang=javascript
 *
 * [4] 寻找两个正序数组的中位数
 */

// @lc code=start
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var findMedianSortedArrays = function(nums1, nums2) {
  const isOdd = (nums1.length + nums2.length) % 2 // 0为偶数 1为奇数
  len = (((nums1.length + nums2.length) / 2) + 1) | 0
  let ary = []
  for (let i = 0; i < len; i++) {
    if (nums1.length === 0) {
      ary.push(nums2.shift())
    } else if (nums2.length === 0 || nums1[0] < nums2[0]) {
      ary.push(nums1.shift())
    } else {
      ary.push(nums2.shift())
    }
  }
  if (isOdd) {
    return ary[len - 1]
  }
  return (ary[len - 1] + ary[len - 2]) / 2
}
// @lc code=end

